package com.example.plantuml.demo.service;

import com.example.plantuml.demo.model.User;
import com.example.plantuml.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;
    
    @Autowired
    private ValidationService validationService;
    @Autowired
    private AbcService abcService;

    public User getUserById(Long id) {
        // 验证参数
        validationService.validateId(id);
        
        // 查询用户
        return userRepository.findById(id)
                .orElseThrow(() -> new IllegalArgumentException("用户不存在: " + id));
    }
    
    public User createUser(User user) {
        // 验证用户信息
        validationService.validateUser(user);
        abcService.aaa(10,"描述信息");

        // 模拟一些业务逻辑处理
        processUserInfo(user);

        // 保存用户
        return userRepository.save(user);
    }
    
    public Iterable<User> getAllUsers() {
        return userRepository.findAll();
    }
    
    // 处理用户信息的辅助方法
    private void processUserInfo(User user) {
        // 模拟处理延迟
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        
        // 可以添加一些用户信息处理逻辑
        if (user.getName() != null) {
            user.setName(user.getName().trim());
        }
    }
}
